#!/usr/bin/python

# imports
import mwclient
import client, page
import re
import os, sys
import hashlib

# Get the title of the section ==FileName.xyz==
# and everything between the <source*> </source> tag
def GetCode(S):
    reg = re.compile(r"==([\w ]*?\.[\w ]*?)==.*?<source.*?>(.*?)</source>.*?", re.DOTALL)
    return [(x.strip('\n'), y.strip('\n')) for (x,y) in reg.findall(S)]

def GetImage(S):
  reg = re.compile("<div class=\"float.*\">\[\[File:(.*)\]\]<\/div>",re.DOTALL)
  return [(x.strip('\n')) for x in reg.findall(S)]

# Connect to the wikix2
site = mwclient.Site(('https','itk.org'), '/Wiki/')

foundPagesWithoutImages = False
for page in site.pages:
  to_find = "VTK/Examples/"
  start = page.name.find(to_find)

  # If the page doesn't start with VTK/Examples, skip it
  if start < 0:
    continue

  # Get the part of the page name that comes after VTK/Examples/
  # e.g. if the page name is VTK/Examples/GeometricObjecs/Line, 
  # ExamplePath will be GeometriObjects/Line
  ExamplePath = page.name[start+len(to_find):]

  # Continuing the above example, the below splits GeometricObjects/Line
  # into PathName = GeometricObjects/
  # ExampleName = Line
  PathSplit = os.path.split(ExamplePath)

  PathName = re.sub("Cxx/","",PathSplit[0])
  PathName = re.sub("/","",PathName)
  ExampleName = PathSplit[1] #not used

  # Get the content of the page
  content = page.edit()

  # Use our fancy regular expression parsing function to get all of the file names and file content on the page
  CodeChunks = GetCode(content)

  for code in CodeChunks:
    if CodeChunks:
      FileName = code[0]
      FileContent = code[1]

    fileSplit = os.path.splitext(FileName)
    imageName = 'VTK_Examples_Baseline_' + PathName + '_' + 'Test' + fileSplit[0] + '.png'
    image=site.Images[imageName]
    if image.imageinfo:
      imageFound = GetImage(content)
      if imageFound:
          try:
              print "------------- found " + imageFound[0]
          except UnicodeEncodeError as e:
              print "************* error " + imageName
      else:
        print imageName + ' exists but is not referenced in the Wiki Example'
        print 'http://vtk.org/Wiki/index.php?title=VTK/Examples/' + ExamplePath + '&action=edit'
        imageRef = '<div class="floatcenter">[[File:' + imageName + ']]</div>'
        print imageRef        
        foundPagesWithoutImages = True
if foundPagesWithoutImages:
        sys.exit(1)
else:
        sys.exit(0)
